﻿Ext.define('GS.view.PostList', {
    extend: 'Ext.Container',
    xtype: 'postindex',
    id: 'postindex',

    updateList: function (postData) {
        this.config.category_id = postData.id;
        Ext.getCmp('postlist').getStore().load();
    },

    config: {
        layout: {
            type: 'vbox'
        },

        items: [
        {
            xtype: 'list',
            flex: 1,
            loadingText: "Loading Posts...",
            emptyText: "<p>Der er endnu ingen indlæg i denne kategori.</p>",
            itemTpl: '{title}',
            category_id: null,
            id: 'postlist',

            store: {
                model: 'GS.model.post.Post',

                fields: [
                    'title', 'body'
                ],

                proxy: {
                    type: 'jsonp',
                    url: GS.util.Config.getServerUrl() + 'GetPosts.php?',
                    reader: {
                        type: 'json',
                        rootProperty: 'items'
                    }
                },

                autoLoad: false,

                listeners: {
                    beforeload: function (store, operation) {
                        var url = GS.util.Config.getServerUrl() + 'GetPosts.php?category_id=' + Ext.getCmp('postindex').config.category_id;
                        store.getProxy().setUrl(url);
                    }
                }
            }
        },
        {
            xtype: 'button',
            text: 'Tilføj Indlæg',
            ui: 'confirm',
            id: 'btnAddPost',
            docked: 'bottom',
            height: '30px',
            margin: 10
        }]
    }
});

